<template>
  <!-- 角色表单弹窗 -->
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="800px"
      draggable
      @close="closeDialog"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :inline="true"
        label-width="100px"
        :rules="rules"
      >
        <div class="search-container">
          <el-form-item
            label="设定编号"
            prop="settingCode"
            style="width: 280px"
          >
            <el-input
              v-model="formData.settingCode"
              placeholder="请输入标准设定编号"
              clearable
            />
          </el-form-item>
          <el-form-item label="检测方案" style="width: 280px">
            <el-select
              :disabled="props.isReadonly"
              @change="schemeChange"
              v-model="formData.schemeId"
              clearable
              placeholder="请选择业务领域"
            >
              <el-option
                v-for="item in schemeList"
                :key="item.id"
                :label="item.schemeName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="业务领域" style="width: 280px">
            <el-select
              v-model="formData.businessDomain"
              placeholder="请选择业务领域"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('BusinessDomain')"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="检测类别" style="width: 280px">
            <el-select
              @change="checkTypeChange"
              v-model="formData.checkTypeId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in checkTypes"
                :key="item.id"
                :label="item.checkTypeName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="公司名称" style="width: 280px">
            <SelectOriganization
              :selectData="formData.organizationId"
              :isCheckStrictly="true"
              @change="getSelectIds"
              @change-data="organizationDataChange"
              :isMultiple="false"
            />
          </el-form-item>
          <el-form-item label="工厂" style="width: 280px">
            <el-select
              @change="plantChange"
              v-model="formData.plantId"
              placeholder="请选择"
            >
              <el-option
                v-for="item in plants"
                :key="item.id"
                :label="item.plantName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="存储地点" style="width: 280px">
            <el-input v-model="formData.storageLocationName" />
          </el-form-item>
          <el-form-item label="物料" style="width: 280px">
            <el-input
              v-model="formData.materialId"
              placeholder="请输入物料"
              clearable
            />
          </el-form-item>
          <el-form-item label="设备型号" style="width: 280px">
            <el-input
              v-model="formData.deviceCode"
              placeholder="请输入设备型号"
              clearable
            />
          </el-form-item>
          <el-form-item label="设备类型" style="width: 280px">
            <el-input
              v-model="formData.deviceSort"
              placeholder="请输入设备类型"
              clearable
            />
          </el-form-item>
          <el-form-item label="备注" style="width: 590px">
            <el-input
              type="textarea"
              v-model="formData.remarks"
              placeholder="请输入备注"
              clearable
            />
          </el-form-item>
        </div>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" :loading="loading" @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMCHECKSTANDARDSETTINGADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let schemeList = ref([]);
let plants = ref([]);
let checkTypes = ref([]);
let formData = reactive({
  id: "",
  settingCode: "",
  schemeId: "",
  schemeCode: "",
  schemeName: "",
  businessDomain: "",
  businessDomainName: "",
  checkTypeId: "",
  checkTypeCode: "",
  checkTypeName: "",
  organizationId: "",
  organizationCode: "",
  organizationName: "",
  plantId: "",
  plantCode: "",
  plantName: "",
  storageLocationId: "",
  storageLocationCode: "",
  storageLocationName: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  deviceCode: "",
  deviceSort: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

/** rules规则 */
const rules = reactive({
  settingCode: [
    { required: true, message: "请输入标准设定编号", trigger: "blur" },
  ],
  schemeId: [{ required: true, message: "请输入检测方案id", trigger: "blur" }],
  schemeCode: [{ required: true, message: "请输入检测方案", trigger: "blur" }],
  businessDomain: [
    { required: true, message: "请输入业务领域", trigger: "blur" },
  ],
  checkTypeId: [
    { required: true, message: "请输入检测类别id", trigger: "blur" },
  ],
  checkTypeName: [
    { required: true, message: "请输入检测类别名称", trigger: "blur" },
  ],
  organizationId: [{ required: true, message: "请输入公司", trigger: "blur" }],
  organizationCode: [
    { required: true, message: "请输入公司code", trigger: "blur" },
  ],
  organizationName: [
    { required: true, message: "请输入公司名称", trigger: "blur" },
  ],
  plantId: [{ required: true, message: "请输入工厂id", trigger: "blur" }],
  plantCode: [{ required: true, message: "请输入工厂code", trigger: "blur" }],
  plantName: [{ required: true, message: "请输入工厂名称", trigger: "blur" }],
  storageLocationId: [
    { required: true, message: "请输入存储地点id", trigger: "blur" },
  ],
  storageLocationCode: [
    { required: true, message: "请输入存储地点code", trigger: "blur" },
  ],
  storageLocationName: [
    { required: true, message: "请输入存储地点名称", trigger: "blur" },
  ],
  materialId: [{ required: true, message: "请输入物料id", trigger: "blur" }],
  materialCode: [
    { required: true, message: "请输入物料code", trigger: "blur" },
  ],
  materialName: [
    { required: true, message: "请输入物料名称", trigger: "blur" },
  ],
  deviceCode: [{ required: true, message: "请输入设备型号", trigger: "blur" }],
  deviceSort: [{ required: true, message: "请输入设备类型", trigger: "blur" }],
  version: [{ required: true, message: "请输入数据版本号", trigger: "blur" }],
  remarks: [{ required: true, message: "请输入备注", trigger: "blur" }],
  createTime: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
  createdUserId: [{ required: true, message: "请输入创建人", trigger: "blur" }],
  createdUserName: [
    { required: true, message: "请输入创建人名", trigger: "blur" },
  ],
  lastUpdateTime: [
    { required: true, message: "请输入更新时间", trigger: "blur" },
  ],
  lastUpdatedUserId: [
    { required: true, message: "请输入更新人", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { required: true, message: "请输入更新人名", trigger: "blur" },
  ],
  deleted: [{ required: true, message: "请输入删除标识", trigger: "blur" }],
});

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    console.log("新增页面", props);
    if (newVal?.id) {
      Object.assign(formData, props.data);
      console.log("111112", formData);
      if (formData.id) {
        getdetails();
        getPlants();
      }
    }
  }
);
function schemeChange() {
  let scheme = schemeList.filter((item) => item.id === formData.schemeId)[0];
  formData.schemeCode = scheme.schemeCode;
  formData.schemeName = scheme.schemeName;
  formData.businessDomain = scheme.businessDomain;
  formData.businessDomainName = scheme.businessDomainName;
  formData.organizationId = scheme.organizationId;
  formData.organizationCode = scheme.organizationCode;
  formData.organizationName = scheme.organizationName;
  formData.plantId = scheme.plantId;
  formData.plantCode = scheme.plantCode;
  formData.plantCode = scheme.plantCode;
  formData.plantName = scheme.plantName;
  formData.checkTypeId = scheme.checkTypeId;
  formData.checkTypeCode = scheme.checkTypeCode;
  formData.checkTypeName = scheme.checkTypeName;
  getPlants();
}
/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.settingCode = "";
  formData.schemeId = "";
  formData.schemeCode = "";
  formData.businessDomain = "";
  formData.checkTypeId = "";
  formData.checkTypeName = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.organizationName = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.plantName = "";
  formData.storageLocationId = "";
  formData.storageLocationCode = "";
  formData.storageLocationName = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialName = "";
  formData.deviceCode = "";
  formData.deviceSort = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 角色保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      console.log("formData", toRaw(formData));
      await request({
        url: "/mdm/checkstandardsetting/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}
async function getSchemeList() {
  let checkScheme = await request({
    url: "/mdm/checkscheme/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  schemeList = checkScheme.data;
}
async function getdetails() {
  let sondetails = await request({
    url: "/mdm/checkstandardsetting/getDetails",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  formData.details = sondetails.data;
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
onMounted(() => {
  getSchemeList();
  getCheckTypes();
});
function getSelectIds(ids) {
  formData.organizationId = ids;
  getPlants();
}
function organizationDataChange(datas: [] | {}) {
  formData.organizationName = datas.organizationName;
  formData.organizationCode = datas.organizationCode;
}
async function getPlants() {
  const data = await request({
    url: "/mdm/plant/getList",
    method: "post",
    data: { organizationId: formData.organizationId },
  });
  plants.value = data.data;
}
async function getCheckTypes() {
  const data = await request({
    url: "/mdm/checktype/getList",
    method: "post",
    data: {},
  });
  checkTypes.value = data.data;
}
async function plantChange() {
  const data = plants.value.filter((item) => item.id === formData.plantId)[0];
  formData.plantCode = data.plantCode;
  formData.plantName = data.plantName;
}
async function checkTypeChange() {
  const data = plants.value.filter((item) => item.id === formData.plantId)[0];
  formData.plantCode = data.plantCode;
  formData.plantName = data.plantName;
}
</script>

<style lang="scss" scoped>
h4 {
  font-weight: 400;
  color: #000;
  padding-left: 10px;
  font-size: 14px;
  line-height: 14px;
}
.formTable {
  :deep(.el-form-item) {
    margin-right: 0 !important;
  }
}
:deep(.el-dialog .el-dialog__body) {
  overflow-y: auto;
  height: 35vh;
}
:deep.el-input.el-input--default.el-input--suffix {
  // 固定宽度
  width: 190px !important;
}
</style>
